package com.flowerplus.store.common.structure.presenter.photo.upload;//package com.quncan.peijue.common.structure.presenter.photo.upload;
//
//import com.jmxchat.im.api.ApiService;
//import com.jmxchat.im.app.chat.constant.MessageConstant;
//import com.jmxchat.im.app.chat.model.ChatTakeVO;
//import com.jmxchat.im.common.data.http.AppSubscriber;
//import com.jmxchat.im.common.execption.utils.ExceptionHandle;
//import com.jmxchat.im.common.structure.mvp.AbsBasePresenter;
//import com.jmxchat.im.common.structure.presenter.photo.uploadImage.vo.UploadResultVo;
//import com.jmxchat.im.data.db.manager.ChatTakeManager;
//import com.jmxchat.im.data.db.manager.UserDbManager;
//import com.jmxchat.im.im.IMManager;
//import com.jmxchat.im.im.model.GroupMessage;
//import com.jmxchat.im.im.model.Message;
//import com.jmxchat.im.models.local.ChatTakePO;
//import com.jmxchat.im.models.result.ResultBean;
//
//import java.util.Date;
//import java.util.List;
//
//import javax.inject.Inject;
//
//import ems358.com.logger.Logger;
//import rx.Observable;
//import rx.Subscriber;
//import rx.Subscription;
//import rx.functions.Func1;
//import rx.functions.Func2;
//
//import static com.jmxchat.im.app.chat.constant.MsgStatusConstant.STATUS_COMPLETE;
//import static com.jmxchat.im.app.chat.constant.MsgStatusConstant.STATUS_ERROR;
//import static com.jmxchat.im.app.chat.constant.MsgStatusConstant.STATUS_ING;
//
///**
// * Created by z on.
// * Des:
// * Date: 2017/4/13
// */
//public class UploadPhotoPresenter extends AbsBasePresenter implements UploadPhotoContract.CountryUtil {
//    private static final String TAG = "UploadPhotoPresenter";
//    private UploadPhotoContract.View mView;
//    private ChatTakeManager mChatTakeManager;
//    private ApiService mApiService;
//    private Subscription mSubscribe;
//    private Subscription mUploadSubscribe;
//
//    @Inject
//    public UploadPhotoPresenter(ApiService apiService, ChatTakeManager chatTakeManager) {
//        mApiService = apiService;
//        mChatTakeManager = chatTakeManager;
//        mUser = UserDbManager.getInstance().loadFirst();
//    }
//
//    @Override
//    public void onCreate(UploadPhotoContract.View view) {
//        this.mView = view;
//    }
//
//    @Override
//    public void onDestroy() {
//        if (mSubscribe != null) mSubscribe.unsubscribe();
//        if (mUploadSubscribe != null) mUploadSubscribe.unsubscribe();
//        mView = null;
//    }
//
//    @Override
//    public void requestRetry() {
//
//    }
//
//    @Override
//    public void uploadImage(final List<ChatTakeVO> chatTakes, final String toUserId, final boolean isGroup) {
//
//        mSubscribe = Observable.from(chatTakes)
//                .flatMap(new Func1<ChatTakeVO, Observable<UploadResultVo>>() {
//                    @Override
//                    public Observable<UploadResultVo> call(ChatTakeVO s) {
//
//                        return Observable.zip(Observable.just(s), mApiService.uploadFile(s.getFilePathLocal()),
//                                new Func2<ChatTakeVO, ResultBean<String>, UploadResultVo>() {
//                                    @Override
//                                    public UploadResultVo call(ChatTakeVO chatTake, ResultBean<String> fileResultResultBean) {
//                                        return new UploadResultVo(chatTake, fileResultResultBean.getParams());
//                                    }
//                                });
//                    }
//                }).map(new Func1<UploadResultVo, UploadResultVo>() {
//                    @Override
//                    public UploadResultVo call(UploadResultVo vo) {
//                        //在这里改变本地数据库的数据
//                        ChatTakeVO chatTake = vo.getChatTake()
//                                .setBody(vo.getFileName())
//                                .setCreateDate(new Date().getTime())
//                                .setMsgStatus(STATUS_COMPLETE);
//                        vo.setChatTake(chatTake);
//                        mChatTakeManager.updateTake(new ChatTakePO(chatTake));
//                        return vo;
//                    }
//                }).subscribe(new Subscriber<UploadResultVo>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        e.printStackTrace();
//                        String msg = ExceptionHandle.handle(e);
//                        Logger.e(TAG, msg);
//                        //改变数据库数据状态  显示错误状态
//                        List<ChatTakePO> takeList = mChatTakeManager.findTakeByStatus(chatTakes.get(0).getHashCode(), STATUS_ING);
//                        if (takeList != null && !takeList.isEmpty()) {
//                            for (ChatTakePO chatTake : takeList) {
//                                chatTake.setMsgStatus(STATUS_ERROR);
//                            }
//                        }
//                        mChatTakeManager.updateTake(takeList);
//                        mView.uploadError(ChatTakeVO.toList(takeList));
//                    }
//
//                    @Override
//                    public void onNext(UploadResultVo vo) {
//                        ChatTakeVO chatTake = vo.getChatTake();
//                        mView.uploadComplete(chatTake);
//                        Message message = new Message();
//                        message.setSourceTime(chatTake.getCreateDate());
//                        message.setMessageBody(chatTake.getBody());
//                        message.setMessageType(MessageConstant.MESSAGE_TYPE_IMAGE.byteValue());
//                        if (isGroup) {
//
//                            GroupMessage groupMessage = new GroupMessage();
//                            groupMessage.setMessageData(message);
//                            groupMessage.setSourceId(mUser.getUserId());
//                            IMManager.getInstance().sendGroupMessage(groupMessage, toUserId);
//                        } else {
//                            IMManager.getInstance().sendMessage(message, toUserId);
//                        }
//                    }
//                });
//    }
//
//    @Override
//    public void uploadImage(String path) {
//        mUploadSubscribe = mApiService.uploadFile(path).subscribe(new AppSubscriber<ResultBean<String>>(mView) {
//            @Override
//            public void next(ResultBean<String> stringResultBean) {
//                mView.uploadComplete(stringResultBean.getParams());
//            }
//        });
//    }
//}
